using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace UWBGL_XNA_Lib13
{
	public class UWB_XNAWindowHandler : UWB_WindowHandler
	{
		protected GraphicsDevice m_XNA_GraphicsDevice;
		protected PresentationParameters pp;

		public override bool IsGraphicsDeviceValid()
		{
			return (m_XNA_GraphicsDevice != null);
		}

		public override bool InitializeHandler(IntPtr hAttachedWindow)
		{
			m_XNA_GraphicsDevice = null;
			pp = null;

			if (!base.InitializeHandler(hAttachedWindow))
				return false;

			UWB_XNAGraphicsSystem.m_TheAPI.CreateGraphicsContext(hAttachedWindow, ref pp);
			m_XNA_GraphicsDevice = UWB_XNAGraphicsSystem.m_TheAPI.GetActiveDevice();
			return true;
		}

		public override void BeginScene()
		{
			if (m_XNA_GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Normal)
			{
				UWB_XNAGraphicsSystem.m_TheAPI.BeginScene(pp);
			}
		}

		public override void EndSceneAndShow()
		{
			if (m_XNA_GraphicsDevice.GraphicsDeviceStatus == GraphicsDeviceStatus.Normal)
				UWB_XNAGraphicsSystem.m_TheAPI.EndSceneAndShow(pp);
		}

		public override void loadW2NDCXform()
		{
			Matrix world2ndc;
			Matrix toSize;
			Matrix toPlace;

			//UWB_XNAGraphicsSystem.m_TheAPI.MyEffect.FogEnabled = true;

			//UWB_XNAGraphicsSystem.m_TheAPI.MyEffect.FogColor = Color.Aqua.ToVector3();

			Vector3 center = m_WCWindow.getCenter();

			toPlace = Matrix.CreateTranslation(-center);
			toSize = Matrix.CreateScale(2.0f / m_WCWindow.width(), 2.0f / m_WCWindow.height(), 1.0f);
			world2ndc = toPlace * toSize;

			UWB_XNAGraphicsSystem.m_TheAPI.setViewMatrix(world2ndc);
		}
	}
}
